<?php

function home_ca() {
	global $smarty;
	return $smarty->fetch("home_ca.html");
}

function process_ca_modules_units() {
	global $smarty, $function;
	$a = isset($_GET['a']) ? $_GET['a'] : "";
	$oModunit = new Modunit();
	$oQbank = new Questionbank();
	
	switch(strtolower($a)) {	
		
		case "u":			
			$list_units = $oModunit->getAllUnits();			
			$smarty->assign("list_units", $list_units);
			$smarty->assign("rs", $rs);
			return $smarty->fetch("modunit/viewu.html");
		break;
		case "m":
			$list_mods = $oModunit->getAllModules();			
			$smarty->assign("list_mods", $list_mods);			
			$smarty->assign("rs", $rs);
			return $smarty->fetch("modunit/viewm.html");
		break;
		case "viewtest":
			$op = isset($_GET['op']) ? intval($_GET['op']) : 1;
			if($op == 1) {
				$mid = isset($_POST['module']) ? intval($_POST['module']) : intval($_GET['m']);
				$list_tests = $oModunit->getAllTestsInModule($mid);
				$smarty->assign("mid", $mid);
				$smarty->assign("unitid", 0);
			} else {
				$unitid = isset($_POST['unit']) ? intval($_POST['unit']) : intval($_GET['u']);
				$list_tests = $oModunit->getAllTestsInUnit($unitid);
				$smarty->assign("mid", 0);
				$smarty->assign("unitid", $unitid);
			}
			$smarty->assign("op", $op);
			$smarty->assign("list_tests", $list_tests);
			return $smarty->fetch("modunit/showtests.html");
		break;
		
		case "addtest":
			if(isset($_POST['btn_submit'])) {
				$mid = intval($_POST['mid']);
				$uid = intval($_POST['unitid']);
				if($mid == 0) {
					$data['ModuleID'] = $oModunit->getMIDfromUID($uid);
					$data['UnitID'] = $uid;
					$op = 2;
				} elseif($uid == 0) {
					$data['ModuleID'] = $mid;
					$data['UnitID'] = NULL;
					$op = 1;
				}
				$data['Title'] = stripslashes($function->FixQuotes($_POST['ttitle']));
				$data['Description'] = stripslashes($function->FixQuotes($_POST['test_desc']));
				$data['Deadline'] = stripslashes($function->FixQuotes($_POST['deadline']));
				$data['TimeLimit'] = intval($_POST['timelimit']); // minutes
				$data['Status'] = 1; // 1: normal, 0: deleted
				$good = $oModunit->insert_Test($data);
				if($good) {
					return $function->goto_url("?mod=camu&a=viewtest&op=$op&m=".$data['ModuleID']."&u=".$data['UnitID']);
				} else {
					return $function->msg_box("Error in processing. Please try again.",2,"?mod=camu&a=addtest&m=".$data['ModuleID']."&u=".$data['UnitID']);
				}
			} else {
				$mid = isset($_GET['m']) ? intval($_GET['m']) : 0;
				$unitid = isset($_GET['u']) ? intval($_GET['u']) : 0;
				$smarty->assign("mid", $mid);
				$smarty->assign("unitid", $unitid);
				return $smarty->fetch("modunit/addtest.html");
			}
		break;
		
		case "edittest":
			if(isset($_POST['btn_submit'])) {
				$data['TestID'] = intval($_POST['tid']);
				$data['ModuleID'] = intval($_POST['mid']);
				$data['UnitID'] = intval($_POST['unitid']);
				if($data['UnitID'] == "") {
					$op = 1;
				} else $op = 2;
				$data['Title'] = stripslashes($function->FixQuotes($_POST['ttitle']));
				$data['Description'] = stripslashes($function->FixQuotes($_POST['test_desc']));
				$data['Deadline'] = stripslashes($function->FixQuotes($_POST['deadline']));
				$data['TimeLimit'] = intval($_POST['timelimit']); // minutes
				$data['Status'] = 1; // 1: normal, 0: deleted
				$good = $oModunit->update_Test($data);
				if($good) {
					return $function->goto_url("?mod=camu&a=viewtest&op=$op&m=".$data['ModuleID']."&u=".$data['UnitID']);
				} else {
					return $function->msg_box("Error in processing. Please try again.",2,"?mod=camu&a=edittest&tid=".$data['TestID']);
				}
			} else {
				$testid = isset($_GET['tid']) ? intval($_GET['tid']) : 0;
				$detail = $oModunit->getTestDetail($testid);
				$smarty->assign("detail", $detail);
				return $smarty->fetch("modunit/edittest.html");
			}
		break;
		
		case "deletetest":
			if(isset($_POST['btn_submit'])) {
				$tid = intval($_POST['tid']);
				$mid = intval($_POST['mid']);
				$unitid = intval($_POST['unitid']);
				if($unitid == "") {
					$op = 1;
				} else $op = 2;
				$ok = intval($_POST['ok']);
				if($ok == 1) {
					$oModunit->delete_Test($tid);
					return $function->msg_box("Delete OK.",2,"?mod=camu&a=viewtest&op=$op&m=".$mid."&u=".$unitid."");
				} else {
					return $function->msg_box("Error in processing. Please try again.",2,"?mod=camu");
				}
			} else {
				$tid = isset($_GET['tid']) ? intval($_GET['tid']) : 0;
				$detail = $oModunit->getTestDetail($tid);
				$smarty->assign("detail", $detail);
				return $smarty->fetch("modunit/deletetest.html");
			}
		break;
		
		case "addquestionbank":
			if(isset($_POST['btn_submit'])) {
				$testid = intval($_POST['testid']);
				$qbids = $_POST['questionbankid'];
				foreach($qbids as $qbid) {
					$qbid = intval($qbid);
					$good = $oModunit->insertQuestionToTest($qbid, $testid);
				}
				$op = $_POST['op'];
				$m = $_POST['m'];
				$u = $_POST['u'];
				if($good) {
					return $function->msg_box("Success!!!",2,"?mod=camu&a=viewtest&op=".$op."&m=".$m."&u=".$u);
				} else {
					return $function->msg_box("Error in processing. Please try again.",2,"?mod=camu&a=addquestionbank&tid=".$testid);
				}
			} else {
				$op = isset($_GET['op']) ? intval($_GET['op']) : 1;
				$m = isset($_GET['m']) ? intval($_GET['m']) : 0;
				$u= isset($_GET['u']) ? intval($_GET['u']) : 0;
				$tid = intval($_GET['tid']);
				$testtitle = stripslashes($function->FixQuotes($_POST['testtitle']));
				$list_question_banks = $oQbank->getAllQuestionsBankExceptAddAlready($tid);
				
				$smarty->assign("list_questions_bank", $list_question_banks);
				$smarty->assign("tid", $tid);
				$smarty->assign("op", $op);
				$smarty->assign("m", $m);
				$smarty->assign("u", $u);
				$smarty->assign("testtitle");
				return $smarty->fetch("modunit/addqbanktotest.html");
			}
 		break;
		
		case "questionintest":
			$tid = intval($_GET['tid']);
			$list_questions = $oModunit->getAllQuestionInTest($tid);
			$smarty->assign("listqt", $list_questions);
			$smarty->assign("m", $_GET['m']);
			$smarty->assign("u", $_GET['u']);
			if ($_GET['m'] != '' && intval($_GET['m']) > 0){
				$smarty->assign("op", 1);
			}else{
				$smarty->assign("op", 0);
			}
			return $smarty->fetch("modunit/questionintest.html");
		break;
		
		case "rmqintest":
			if(isset($_POST['btn_submit'])) {
				$tid = intval($_POST['tid']);
				$qbid = intval($_POST['qbid']);
				$ok = intval($_POST['ok']);
				if($ok == 1) {
					$oModunit->deleteQuestionInTest($tid, $qbid);
					return $function->msg_box("Delete OK.",2,"?mod=camu&a=questionintest&tid=".$tid);
				} else $function->msg_box("Error in processing. Please try again.",2,"?mod=camu&a=questionintest&tid=".$tid);
			} else {
				return $smarty->fetch("modunit/deletequestionintest.html");
			}
		break;
	}
}

?>